package com.lzlg.lzlglab.web;

import com.github.pagehelper.PageInfo;
import com.lzlg.lzlglab.entity.SmartLabs;
import com.lzlg.lzlglab.service.SmartLabsService;
import com.lzlg.lzlglab.utils.Response;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
    1、注册Bean -》Controller
    2、添加前端URL的映射：一个url 对应 一个方法
        类上：前缀
        方法上：对应
    3、调用Service的实现
 */
@RestController
@RequestMapping("/labs")
@Api("实验室管理模块")
public class SmartLabsController {

    // 调用Service
    @Autowired
    SmartLabsService smartLabsService;

    @ApiOperation("获取所有实验室信息")
    @GetMapping("/all")
    public Response<List<SmartLabs>> findAllLabs(){
        return Response.ok(smartLabsService.findAllSmartLabs());
    }

    @ApiOperation("新增/编辑实验室信息")
    @PostMapping("/lab")
    public Response saveOrUpdateLabs(@RequestBody SmartLabs smartLabs){

        smartLabsService.saveOrUpdateLabs(smartLabs);
        return Response.ok("操作成功");
    }

    @ApiOperation("根据id删除一条实验室信息")
    @DeleteMapping("/{id}")
    public Response removeLab(@PathVariable String id){
        smartLabsService.removeLabsById(id);
        return Response.ok("删除成功");
    }
    @ApiOperation("根据id数组批量删除实验室信息")
    @DeleteMapping("/batch")
    public Response removeLab(@RequestBody List<String> ids){
        smartLabsService.removeLabsByIds(ids);
        return Response.ok("删除成功");
    }

    @ApiOperation("分页查询实验室信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页码",name = "pageNum",paramType = "query"),
            @ApiImplicitParam(value = "当前条数",name = "pageSize",paramType = "query"),
            @ApiImplicitParam(value = "实验室名称",name = "key",paramType = "query"),
            @ApiImplicitParam(value = "数量",name = "typeId",paramType = "query")
    })
    @GetMapping("/by/condition")
    public Response findLabsByPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(required = false) String key,
                                   @RequestParam(required = false) String typeId){
        PageInfo<SmartLabs> labsByPage = smartLabsService.findLabsByPage(pageNum, pageSize, key, typeId);
        return Response.ok(labsByPage);
    }
}
